package com.example.exceptionhandler.common;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

@ControllerAdvice
@ResponseBody
public class MyExceptionAdivce {
    @ExceptionHandler(NullPointerException.class)
    public HashMap<String,Object> doNullPointerException(NullPointerException e){
        HashMap<String,Object> result = new HashMap<>();
        result.put("code",-1);
        result.put("msg","空指针异常：" + e.getMessage());
        result.put("data",null);
        return result;
    }

    @ExceptionHandler(Exception.class)
    public HashMap<String,Object> doException(Exception e){
        HashMap<String,Object> result = new HashMap<>();
        result.put("code",-1);
        result.put("msg","存在异常：" + e.getMessage());
        result.put("data",null);
        return result;
    }
}
